{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**复习**:这门课程得主要目的是通过真实的数据，以实战的方式了解数据分析的流程和熟悉数据分析python的基本操作。知道了课程的目的之后，我们接下来我们要正式的开始数据分析的实战教学，完成kaggle上[泰坦尼克的任务](https://www.kaggle.com/c/titanic/overview)，实战数据分析全流程。\n",
    "这里有两份资料：\n",
    "教材《Python for Data Analysis》和 baidu.com &\n",
    "google.com（善用搜索引擎）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 第一章：数据载入及初步观察"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 载入数据\n",
    "数据集下载 https://www.kaggle.com/c/titanic/overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1.1 任务一：导入numpy和pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#写入代码\n",
    "import numpy as np\n",
    "import pandas as pd\n"
   ]
  },
  {
   "source": [
    "【提示】如果加载失败，学会如何在你的python环境下安装numpy和pandas这两个库"
   ],
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1.2 任务二：载入数据\n",
    "(1) 使用相对路径载入数据  \n",
    "(2) 使用绝对路径载入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#写入代码\n",
    "df = pd.read_csv('train.csv')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>PassengerId</th>\n      <th>Survived</th>\n      <th>Pclass</th>\n      <th>Name</th>\n      <th>Sex</th>\n      <th>Age</th>\n      <th>SibSp</th>\n      <th>Parch</th>\n      <th>Ticket</th>\n      <th>Fare</th>\n      <th>Cabin</th>\n      <th>Embarked</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>0</td>\n      <td>3</td>\n      <td>Braund, Mr. Owen Harris</td>\n      <td>male</td>\n      <td>22.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>A/5 21171</td>\n      <td>7.2500</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>1</td>\n      <td>1</td>\n      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n      <td>female</td>\n      <td>38.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>PC 17599</td>\n      <td>71.2833</td>\n      <td>C85</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>1</td>\n      <td>3</td>\n      <td>Heikkinen, Miss. Laina</td>\n      <td>female</td>\n      <td>26.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>STON/O2. 3101282</td>\n      <td>7.9250</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "#写入代码\n",
    "df = pd.read_csv('C:\\\\Users\\\\Xgdyp\\\\Desktop\\\\hands-on-data-analysis\\\\第一单元项目集合\\\\train.csv')\n",
    "df.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【提示】相对路径载入报错时，尝试使用os.getcwd()查看当前工作目录。  \n",
    "【思考】知道数据加载的方法后，试试pd.read_csv()和pd.read_table()的不同，如果想让他们效果一样，需要怎么做？了解一下'.tsv'和'.csv'的不同，如何加载这两个数据集？  \n",
    "【总结】加载的数据是所有工作的第一步，我们的工作会接触到不同的数据格式（eg:.csv;.tsv;.xlsx）,但是加载的方法和思路都是一样的，在以后工作和做项目的过程中，遇到之前没有碰到的问题，要多多查资料吗，使用googel，了解业务逻辑，明白输入和输出是什么。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1.3 任务三：每1000行为一个数据模块，逐块读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<pandas.io.parsers.TextFileReader at 0x1df7ecf1490>"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "#写入代码\n",
    "df = pd.read_csv('train.csv', chunksize=1000)\n",
    "#使用chunk的原因是如果文件太大可能一次不能读入内存中，因此多次读入\n",
    "#使用chunksize index 会乱掉，可以reset_index\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【思考】什么是逐块读取？为什么要逐块读取呢？\n",
    "\n",
    "【提示】大家可以chunker(数据块)是什么类型？用`for`循环打印出来出处具体的样子是什么？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1.4 任务四：将表头改成中文，索引改为乘客ID [对于某些英文资料，我们可以通过翻译来更直观的熟悉我们的数据]\n",
    "PassengerId => 乘客ID  \n",
    "Survived    => 是否幸存   \n",
    "Pclass      => 乘客等级(1/2/3等舱位)  \n",
    "Name        => 乘客姓名  \n",
    "Sex         => 性别                 \n",
    "Age         => 年龄                 \n",
    "SibSp       => 堂兄弟/妹个数  \n",
    "Parch       => 父母与小孩个数  \n",
    "Ticket      => 船票信息             \n",
    "Fare        => 票价                \n",
    "Cabin       => 客舱                \n",
    "Embarked    => 登船港口             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      是否幸存   乘客等级(1/2/3等舱位)  \\\n",
       "乘客ID                          \n",
       "1        0                3   \n",
       "2        1                1   \n",
       "3        1                3   \n",
       "4        1                1   \n",
       "5        0                3   \n",
       "\n",
       "                                                   乘客姓名      性别    年龄  \\\n",
       "乘客ID                                                                    \n",
       "1                               Braund, Mr. Owen Harris    male  22.0   \n",
       "2     Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0   \n",
       "3                                Heikkinen, Miss. Laina  female  26.0   \n",
       "4          Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0   \n",
       "5                              Allen, Mr. William Henry    male  35.0   \n",
       "\n",
       "      堂兄弟/妹个数  父母子女个数              船票信息       票价    客舱 登船港口  \n",
       "乘客ID                                                         \n",
       "1           1       0         A/5 21171   7.2500   NaN    S  \n",
       "2           1       0          PC 17599  71.2833   C85    C  \n",
       "3           0       0  STON/O2. 3101282   7.9250   NaN    S  \n",
       "4           1       0            113803  53.1000  C123    S  \n",
       "5           0       0            373450   8.0500   NaN    S  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>是否幸存</th>\n      <th>乘客等级(1/2/3等舱位)</th>\n      <th>乘客姓名</th>\n      <th>性别</th>\n      <th>年龄</th>\n      <th>堂兄弟/妹个数</th>\n      <th>父母子女个数</th>\n      <th>船票信息</th>\n      <th>票价</th>\n      <th>客舱</th>\n      <th>登船港口</th>\n    </tr>\n    <tr>\n      <th>乘客ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Braund, Mr. Owen Harris</td>\n      <td>male</td>\n      <td>22.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>A/5 21171</td>\n      <td>7.2500</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>1</td>\n      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n      <td>female</td>\n      <td>38.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>PC 17599</td>\n      <td>71.2833</td>\n      <td>C85</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>3</td>\n      <td>Heikkinen, Miss. Laina</td>\n      <td>female</td>\n      <td>26.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>STON/O2. 3101282</td>\n      <td>7.9250</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1</td>\n      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n      <td>female</td>\n      <td>35.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>113803</td>\n      <td>53.1000</td>\n      <td>C123</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Allen, Mr. William Henry</td>\n      <td>male</td>\n      <td>35.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>373450</td>\n      <td>8.0500</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "#写入代码\n",
    "df = pd.read_csv('train.csv', index_col='乘客ID',names=['乘客ID','是否幸存',' 乘客等级(1/2/3等舱位)','乘客姓名','性别','年龄','堂兄弟/妹个数','父母子女个数','船票信息','票价','客舱','登船港口'],header=0)\n",
    "df.head(5)\n"
   ]
  },
  {
   "source": [
    "【思考】所谓将表头改为中文其中一个思路是：将英文列名表头替换成中文。还有其他的方法吗？\n",
    "1. 同故宫"
   ],
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 初步观察\n",
    "导入数据后，你可能要对数据的整体结构和样例进行概览，比如说，数据大小、有多少列，各列都是什么格式的，是否包含null等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2.1 任务一：查看数据的基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\nInt64Index: 891 entries, 1 to 891\nData columns (total 11 columns):\n #   Column           Non-Null Count  Dtype  \n---  ------           --------------  -----  \n 0   是否幸存             891 non-null    int64  \n 1    乘客等级(1/2/3等舱位)  891 non-null    int64  \n 2   乘客姓名             891 non-null    object \n 3   性别               891 non-null    object \n 4   年龄               714 non-null    float64\n 5   堂兄弟/妹个数          891 non-null    int64  \n 6   父母子女个数           891 non-null    int64  \n 7   船票信息             891 non-null    object \n 8   票价               891 non-null    float64\n 9   客舱               204 non-null    object \n 10  登船港口             889 non-null    object \ndtypes: float64(2), int64(4), object(5)\nmemory usage: 83.5+ KB\n"
     ]
    }
   ],
   "source": [
    "#写入代码\n",
    "df.info()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【提示】有多个函数可以这样做，你可以做一下总结\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2.2 任务二：观察表格前10行的数据和后15行的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      是否幸存   乘客等级(1/2/3等舱位)  \\\n",
       "乘客ID                          \n",
       "1        0                3   \n",
       "2        1                1   \n",
       "3        1                3   \n",
       "4        1                1   \n",
       "5        0                3   \n",
       "6        0                3   \n",
       "7        0                1   \n",
       "8        0                3   \n",
       "9        1                3   \n",
       "10       1                2   \n",
       "\n",
       "                                                   乘客姓名      性别    年龄  \\\n",
       "乘客ID                                                                    \n",
       "1                               Braund, Mr. Owen Harris    male  22.0   \n",
       "2     Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0   \n",
       "3                                Heikkinen, Miss. Laina  female  26.0   \n",
       "4          Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0   \n",
       "5                              Allen, Mr. William Henry    male  35.0   \n",
       "6                                      Moran, Mr. James    male   NaN   \n",
       "7                               McCarthy, Mr. Timothy J    male  54.0   \n",
       "8                        Palsson, Master. Gosta Leonard    male   2.0   \n",
       "9     Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)  female  27.0   \n",
       "10                  Nasser, Mrs. Nicholas (Adele Achem)  female  14.0   \n",
       "\n",
       "      堂兄弟/妹个数  父母子女个数              船票信息       票价    客舱 登船港口  \n",
       "乘客ID                                                         \n",
       "1           1       0         A/5 21171   7.2500   NaN    S  \n",
       "2           1       0          PC 17599  71.2833   C85    C  \n",
       "3           0       0  STON/O2. 3101282   7.9250   NaN    S  \n",
       "4           1       0            113803  53.1000  C123    S  \n",
       "5           0       0            373450   8.0500   NaN    S  \n",
       "6           0       0            330877   8.4583   NaN    Q  \n",
       "7           0       0             17463  51.8625   E46    S  \n",
       "8           3       1            349909  21.0750   NaN    S  \n",
       "9           0       2            347742  11.1333   NaN    S  \n",
       "10          1       0            237736  30.0708   NaN    C  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>是否幸存</th>\n      <th>乘客等级(1/2/3等舱位)</th>\n      <th>乘客姓名</th>\n      <th>性别</th>\n      <th>年龄</th>\n      <th>堂兄弟/妹个数</th>\n      <th>父母子女个数</th>\n      <th>船票信息</th>\n      <th>票价</th>\n      <th>客舱</th>\n      <th>登船港口</th>\n    </tr>\n    <tr>\n      <th>乘客ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Braund, Mr. Owen Harris</td>\n      <td>male</td>\n      <td>22.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>A/5 21171</td>\n      <td>7.2500</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>1</td>\n      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n      <td>female</td>\n      <td>38.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>PC 17599</td>\n      <td>71.2833</td>\n      <td>C85</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>3</td>\n      <td>Heikkinen, Miss. Laina</td>\n      <td>female</td>\n      <td>26.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>STON/O2. 3101282</td>\n      <td>7.9250</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>1</td>\n      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n      <td>female</td>\n      <td>35.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>113803</td>\n      <td>53.1000</td>\n      <td>C123</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Allen, Mr. William Henry</td>\n      <td>male</td>\n      <td>35.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>373450</td>\n      <td>8.0500</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Moran, Mr. James</td>\n      <td>male</td>\n      <td>NaN</td>\n      <td>0</td>\n      <td>0</td>\n      <td>330877</td>\n      <td>8.4583</td>\n      <td>NaN</td>\n      <td>Q</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>0</td>\n      <td>1</td>\n      <td>McCarthy, Mr. Timothy J</td>\n      <td>male</td>\n      <td>54.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>17463</td>\n      <td>51.8625</td>\n      <td>E46</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Palsson, Master. Gosta Leonard</td>\n      <td>male</td>\n      <td>2.0</td>\n      <td>3</td>\n      <td>1</td>\n      <td>349909</td>\n      <td>21.0750</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>1</td>\n      <td>3</td>\n      <td>Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)</td>\n      <td>female</td>\n      <td>27.0</td>\n      <td>0</td>\n      <td>2</td>\n      <td>347742</td>\n      <td>11.1333</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>1</td>\n      <td>2</td>\n      <td>Nasser, Mrs. Nicholas (Adele Achem)</td>\n      <td>female</td>\n      <td>14.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>237736</td>\n      <td>30.0708</td>\n      <td>NaN</td>\n      <td>C</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "#写入代码\n",
    "df.head(10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      是否幸存   乘客等级(1/2/3等舱位)                                           乘客姓名  \\\n",
       "乘客ID                                                                         \n",
       "877      0                3                  Gustafsson, Mr. Alfred Ossian   \n",
       "878      0                3                           Petroff, Mr. Nedelio   \n",
       "879      0                3                             Laleff, Mr. Kristo   \n",
       "880      1                1  Potter, Mrs. Thomas Jr (Lily Alexenia Wilson)   \n",
       "881      1                2   Shelley, Mrs. William (Imanita Parrish Hall)   \n",
       "882      0                3                             Markun, Mr. Johann   \n",
       "883      0                3                   Dahlberg, Miss. Gerda Ulrika   \n",
       "884      0                2                  Banfield, Mr. Frederick James   \n",
       "885      0                3                         Sutehall, Mr. Henry Jr   \n",
       "886      0                3           Rice, Mrs. William (Margaret Norton)   \n",
       "887      0                2                          Montvila, Rev. Juozas   \n",
       "888      1                1                   Graham, Miss. Margaret Edith   \n",
       "889      0                3       Johnston, Miss. Catherine Helen \"Carrie\"   \n",
       "890      1                1                          Behr, Mr. Karl Howell   \n",
       "891      0                3                            Dooley, Mr. Patrick   \n",
       "\n",
       "          性别    年龄  堂兄弟/妹个数  父母子女个数              船票信息       票价    客舱 登船港口  \n",
       "乘客ID                                                                       \n",
       "877     male  20.0        0       0              7534   9.8458   NaN    S  \n",
       "878     male  19.0        0       0            349212   7.8958   NaN    S  \n",
       "879     male   NaN        0       0            349217   7.8958   NaN    S  \n",
       "880   female  56.0        0       1             11767  83.1583   C50    C  \n",
       "881   female  25.0        0       1            230433  26.0000   NaN    S  \n",
       "882     male  33.0        0       0            349257   7.8958   NaN    S  \n",
       "883   female  22.0        0       0              7552  10.5167   NaN    S  \n",
       "884     male  28.0        0       0  C.A./SOTON 34068  10.5000   NaN    S  \n",
       "885     male  25.0        0       0   SOTON/OQ 392076   7.0500   NaN    S  \n",
       "886   female  39.0        0       5            382652  29.1250   NaN    Q  \n",
       "887     male  27.0        0       0            211536  13.0000   NaN    S  \n",
       "888   female  19.0        0       0            112053  30.0000   B42    S  \n",
       "889   female   NaN        1       2        W./C. 6607  23.4500   NaN    S  \n",
       "890     male  26.0        0       0            111369  30.0000  C148    C  \n",
       "891     male  32.0        0       0            370376   7.7500   NaN    Q  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>是否幸存</th>\n      <th>乘客等级(1/2/3等舱位)</th>\n      <th>乘客姓名</th>\n      <th>性别</th>\n      <th>年龄</th>\n      <th>堂兄弟/妹个数</th>\n      <th>父母子女个数</th>\n      <th>船票信息</th>\n      <th>票价</th>\n      <th>客舱</th>\n      <th>登船港口</th>\n    </tr>\n    <tr>\n      <th>乘客ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>877</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Gustafsson, Mr. Alfred Ossian</td>\n      <td>male</td>\n      <td>20.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>7534</td>\n      <td>9.8458</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>878</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Petroff, Mr. Nedelio</td>\n      <td>male</td>\n      <td>19.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>349212</td>\n      <td>7.8958</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>879</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Laleff, Mr. Kristo</td>\n      <td>male</td>\n      <td>NaN</td>\n      <td>0</td>\n      <td>0</td>\n      <td>349217</td>\n      <td>7.8958</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>880</th>\n      <td>1</td>\n      <td>1</td>\n      <td>Potter, Mrs. Thomas Jr (Lily Alexenia Wilson)</td>\n      <td>female</td>\n      <td>56.0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>11767</td>\n      <td>83.1583</td>\n      <td>C50</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>881</th>\n      <td>1</td>\n      <td>2</td>\n      <td>Shelley, Mrs. William (Imanita Parrish Hall)</td>\n      <td>female</td>\n      <td>25.0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>230433</td>\n      <td>26.0000</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>882</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Markun, Mr. Johann</td>\n      <td>male</td>\n      <td>33.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>349257</td>\n      <td>7.8958</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>883</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Dahlberg, Miss. Gerda Ulrika</td>\n      <td>female</td>\n      <td>22.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>7552</td>\n      <td>10.5167</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>884</th>\n      <td>0</td>\n      <td>2</td>\n      <td>Banfield, Mr. Frederick James</td>\n      <td>male</td>\n      <td>28.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>C.A./SOTON 34068</td>\n      <td>10.5000</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>885</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Sutehall, Mr. Henry Jr</td>\n      <td>male</td>\n      <td>25.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>SOTON/OQ 392076</td>\n      <td>7.0500</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>886</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Rice, Mrs. William (Margaret Norton)</td>\n      <td>female</td>\n      <td>39.0</td>\n      <td>0</td>\n      <td>5</td>\n      <td>382652</td>\n      <td>29.1250</td>\n      <td>NaN</td>\n      <td>Q</td>\n    </tr>\n    <tr>\n      <th>887</th>\n      <td>0</td>\n      <td>2</td>\n      <td>Montvila, Rev. Juozas</td>\n      <td>male</td>\n      <td>27.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>211536</td>\n      <td>13.0000</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>888</th>\n      <td>1</td>\n      <td>1</td>\n      <td>Graham, Miss. Margaret Edith</td>\n      <td>female</td>\n      <td>19.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>112053</td>\n      <td>30.0000</td>\n      <td>B42</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>889</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Johnston, Miss. Catherine Helen \"Carrie\"</td>\n      <td>female</td>\n      <td>NaN</td>\n      <td>1</td>\n      <td>2</td>\n      <td>W./C. 6607</td>\n      <td>23.4500</td>\n      <td>NaN</td>\n      <td>S</td>\n    </tr>\n    <tr>\n      <th>890</th>\n      <td>1</td>\n      <td>1</td>\n      <td>Behr, Mr. Karl Howell</td>\n      <td>male</td>\n      <td>26.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>111369</td>\n      <td>30.0000</td>\n      <td>C148</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>891</th>\n      <td>0</td>\n      <td>3</td>\n      <td>Dooley, Mr. Patrick</td>\n      <td>male</td>\n      <td>32.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>370376</td>\n      <td>7.7500</td>\n      <td>NaN</td>\n      <td>Q</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "#写入代码\n",
    "\n",
    "df.tail(15)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2.4 任务三：判断数据是否为空，为空的地方返回True，其余地方返回False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "       是否幸存   乘客等级(1/2/3等舱位)   乘客姓名     性别     年龄  堂兄弟/妹个数  父母子女个数   船票信息  \\\n",
       "乘客ID                                                                        \n",
       "1     False            False  False  False  False    False   False  False   \n",
       "2     False            False  False  False  False    False   False  False   \n",
       "3     False            False  False  False  False    False   False  False   \n",
       "4     False            False  False  False  False    False   False  False   \n",
       "5     False            False  False  False  False    False   False  False   \n",
       "...     ...              ...    ...    ...    ...      ...     ...    ...   \n",
       "887   False            False  False  False  False    False   False  False   \n",
       "888   False            False  False  False  False    False   False  False   \n",
       "889   False            False  False  False   True    False   False  False   \n",
       "890   False            False  False  False  False    False   False  False   \n",
       "891   False            False  False  False  False    False   False  False   \n",
       "\n",
       "         票价     客舱   登船港口  \n",
       "乘客ID                       \n",
       "1     False   True  False  \n",
       "2     False  False  False  \n",
       "3     False   True  False  \n",
       "4     False  False  False  \n",
       "5     False   True  False  \n",
       "...     ...    ...    ...  \n",
       "887   False   True  False  \n",
       "888   False  False  False  \n",
       "889   False   True  False  \n",
       "890   False  False  False  \n",
       "891   False   True  False  \n",
       "\n",
       "[891 rows x 11 columns]"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>是否幸存</th>\n      <th>乘客等级(1/2/3等舱位)</th>\n      <th>乘客姓名</th>\n      <th>性别</th>\n      <th>年龄</th>\n      <th>堂兄弟/妹个数</th>\n      <th>父母子女个数</th>\n      <th>船票信息</th>\n      <th>票价</th>\n      <th>客舱</th>\n      <th>登船港口</th>\n    </tr>\n    <tr>\n      <th>乘客ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>887</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>888</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>889</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>890</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>891</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n<p>891 rows × 11 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "#写入代码\n",
    "df.isnull()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【总结】上面的操作都是数据分析中对于数据本身的观察"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【思考】对于一个数据，还可以从哪些方面来观察？找找答案，这个将对下面的数据分析有很大的帮助"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 保存数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.3.1 任务一：将你加载并做出改变的数据，在工作目录下保存为一个新文件train_chinese.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "#写入代码\n",
    "# 注意：不同的操作系统保存下来可能会有乱码。大家可以加入`encoding='GBK' 或者 ’encoding = ’uft-8‘‘`\n",
    "df.to_csv('train_chinese.csv',encoding = 'utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【总结】数据的加载以及入门，接下来就要接触数据本身的运算，我们将主要掌握numpy和pandas在工作和项目场景的运用。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "582px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "interpreter": {
   "hash": "96eb50e1d44aed467dc8f759cb08c32fbfa9babcf79c554e2d0e5feb04653a10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}